package com.flysky.dao.finance.impl;

import java.util.Calendar;
import java.util.HashMap;
import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;

import com.flysky.common.ToolUtil;
import com.flysky.dao.BaseDao;
import com.flysky.dao.finance.IFinanceDao;
import com.flysky.model.common.PagingModel;
import com.flysky.model.finance.BigTypeHbmModel;
import com.flysky.model.finance.FinanceHbmModel;
import com.flysky.model.finance.SmallTypeHbmModel;

public class FinanceDaoImpl extends BaseDao implements IFinanceDao {

	@SuppressWarnings("unchecked")
	@Override
	public List<BigTypeHbmModel> getAllFinanceBigType() {
		Session session = null;
		List<BigTypeHbmModel> bigTypeList = null;
		try{
			session = this.sessionFactory.openSession();
			Criteria criteria = session.createCriteria(BigTypeHbmModel.class);
			criteria.addOrder(Order.asc("type_id"));
			bigTypeList = criteria.list();
		}finally{
			if(session != null){
				session.close();
			}
		}
		return bigTypeList;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<SmallTypeHbmModel> getAllFinanceSmallType() {
		Session session = null;
		List<SmallTypeHbmModel> smallTypeList = null;
		try{
			session = this.sessionFactory.openSession();
			Criteria criteria = session.createCriteria(SmallTypeHbmModel.class);
			smallTypeList = criteria.list();
		}finally{
			if(session != null){
				session.close();
			}
		}
		return smallTypeList;
	}

	@Override
	public BigTypeHbmModel addFinanceBigType(BigTypeHbmModel bigType) {
		Session session = null;
		Transaction tx = null;
		try{
			session = this.sessionFactory.openSession();
			tx = session.beginTransaction();
			session.save(bigType);
			tx.commit();
		}finally{
			if(session != null){
				session.close();
			}
		}
		return bigType;
	}

	@Override
	public SmallTypeHbmModel addFinanceSmallType(SmallTypeHbmModel smallType) {
		Session session = null;
		Transaction tx = null;
		try{
			session = this.sessionFactory.openSession();
			tx = session.beginTransaction();
			session.save(smallType);
			tx.commit();
		}finally{
			if(null != session){
				session.close();
			}
		}
		return smallType;
	}

	@Override
	public void deleteFinanceBigType(BigTypeHbmModel bigType) {
		Session session = null;
		Transaction tx = null;
		try{
			session = this.sessionFactory.openSession();
			tx = session.beginTransaction();
			session.delete(bigType);
			tx.commit();
		}finally{
			if(null != session){
				session.close();
			}
		}
	}

	@Override
	public void deleteFianceSmallType(SmallTypeHbmModel smallType) {
		Session session = null;
		Transaction tx = null;
		try{
			session = this.sessionFactory.openSession();
			tx = session.beginTransaction();
			session.delete(smallType);
			tx.commit();
		}finally{
			if(null != session){
				session.close();
			}
		}
	}

	@Override
	public BigTypeHbmModel getFinanceBigType(int type_id) {
		Session session = null;
		BigTypeHbmModel result = null;
		try{
			session = this.sessionFactory.openSession();
			Query query = session.createQuery("from BigTypeHbmModel as bigType" +
					" where bigType.type_id = :type_id");
			query.setInteger("type_id", type_id);
			result = (BigTypeHbmModel) query.uniqueResult();
		}finally{
			if(null != session){
				session.close();
			}
		}
		return result;
	}

	@Override
	public SmallTypeHbmModel getFinanceSmallType(int type_id) {
		Session session = null;
		SmallTypeHbmModel result = null;
		try{
			session = this.sessionFactory.openSession();
			Query query = session.createQuery("from SmallTypeHbmModel as smallType" +
					" where smallType.type_id = :type_id");
			query.setInteger("type_id", type_id);
			result = (SmallTypeHbmModel) query.uniqueResult();
		}finally{
			if(null != session){
				session.close();
			}
		}
		return result;
	}

	@Override
	public FinanceHbmModel addFinance(FinanceHbmModel financeModel) {
		Session session = null;
		Transaction tx = null;
		try{
			session = this.sessionFactory.openSession();
			tx = session.beginTransaction();
			session.save(financeModel);
			tx.commit();
		}finally{
			if(null != session){
				session.close();
			}
		}
		return financeModel;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<FinanceHbmModel> getAllMatchedFinances(
			HashMap<String, String> param) {
		Session session = null;
		List<FinanceHbmModel> resultList = null;
		try{
			session = this.sessionFactory.openSession();
			//build start and end date
			Calendar startDat = Calendar.getInstance();
			Calendar endDat = Calendar.getInstance();
			startDat.set(Integer.parseInt(param.get("YEAR")), Integer.parseInt(param.get("MONTH")), 1, 0, 0, 0);
			endDat.set(Integer.parseInt(param.get("YEAR")), Integer.parseInt(param.get("MONTH")), 
					startDat.getActualMaximum(Calendar.DAY_OF_MONTH), 23, 59, 59);
			Criteria critiria = session.createCriteria(FinanceHbmModel.class);
			if(ToolUtil.isEmpty(param.get("paging"))){
				critiria.addOrder(Order.desc("financeDate"));
				critiria.addOrder(Order.desc("createDate"));
			}else{
				critiria.addOrder(Order.asc("financeDate"));
			}
			critiria.add(Restrictions.eq("maker", param.get("MAKER")));
			critiria.add(Restrictions.between("financeDate", startDat.getTime(), endDat.getTime()));
			if(ToolUtil.isEmpty(param.get("paging"))){
				critiria.setFirstResult(Integer.parseInt(param.get("START"))-1);
				critiria.setMaxResults(Integer.parseInt(PagingModel.perPage.toString()));
			}
			resultList = critiria.list();
		}finally{
			if(null != session){
				session.close();
			}
		}
		return resultList;
	}

	@Override
	public Long countAllMatchFinances(HashMap<String, String> param) {
		Session session = null;
		long count = 0l;
		try{
			session = this.sessionFactory.openSession();
			String hql = "select count(*) as ct from FinanceHbmModel as finance where finance.maker = :maker";	
			Query query = session.createQuery(hql);
			query.setString("maker", param.get("MAKER"));
			Object result = query.uniqueResult();
			count = Long.parseLong(result.toString());
		}finally{
			if(null != session){
				session.close();
			}
		}
		return count;
	}

}
